package com.sinacom.ibuilding.action;



import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.sinacom.ibuilding.beans.Member;
import com.sinacom.ibuilding.beans.Role;
import com.sinacom.ibuilding.service.MemberService;
import com.sinacom.ibuilding.service.NewsService;

public class LoginAction {
	
	private MemberService memberService = new MemberService();
	private NewsService newsService = new NewsService();

	private String loginName;
	private String loginPwd;
	private int page;
	
	private String oldPwd;
	private String newPwd;
	public String login()throws Exception{
		Member member = memberService.login(loginName, loginPwd); 
		HttpServletRequest request = ServletActionContext.getRequest();
		if(member != null){
			Role role = memberService.getMemberRole(member.getId());  
			//判断用户是否有登录的权限 
			if(null != role && role.getLogin()!=null && role.getLogin().booleanValue()){ 
				member.setRole(role);
				
				if(member.getLogined().booleanValue()){
					request.getSession().setAttribute("member", member);
					String redirectionUrl = (String) request.getSession().getAttribute("redirectionUrl");
					request.getSession().removeAttribute("redirectionUrl");
					if (StringUtils.isEmpty(redirectionUrl)) {
						ServletActionContext.getResponse().sendRedirect("index!index.do");
						return null;
					} else {
						ServletActionContext.getResponse().sendRedirect(redirectionUrl);
						return null;
					}
				}else{
					request.setAttribute("logined", "false");
					return "tologin";
				}
			}else{
				request.setAttribute("error", "您没有登录权限");
				return "tologin";
			}
			
			
		}
		request.setAttribute("error", "错误的用户名和密码");
		return "tologin";
	}
	
	public String modifypwd()throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        pw = response.getWriter();
        Map map = new HashMap();  
		if(StringUtils.isEmpty(oldPwd) || StringUtils.isEmpty(newPwd)){
			map.put("success", "false");
	        map.put("msg", "旧密码或新密码不能为空");
	        pw.print(JSONObject.fromObject(map));
	        return null;
	    }
    	if(oldPwd.equals(newPwd)){
    		map.put("success", "false");
	        map.put("msg", "新密码不能和旧密码相同");
	        pw.print(JSONObject.fromObject(map));
	        return null;
    	}
    	Member member = memberService.login(loginName, oldPwd); 
    	if(member == null){
    		map.put("success", "false");
	        map.put("msg", "旧密码不正确");
	        pw.print(JSONObject.fromObject(map));
	        return null;
    	}
    	
    	member.setPassword(DigestUtils.md5Hex(newPwd));
    	member.setLogined(new Boolean(true));
    	if(memberService.updateMember(member)){
    		map.put("success", "true");
	        map.put("msg", "修改密码成功");
	        pw.print(JSONObject.fromObject(map));
	        return null;
    	}else{
    		map.put("success", "true");
	        map.put("msg", "修改密码失败");
	        pw.print(JSONObject.fromObject(map));
	        return null;
    	}
		
	}
	
	public String logout()throws Exception{
	    HttpServletRequest request = ServletActionContext.getRequest(); 
        Member member = (Member) request.getSession().getAttribute("member");
        if(member != null){  
           request.getSession().removeAttribute("member");
        }
        return "tologin";
    }
	
	public String tologin()throws Exception{
		
		return "tologin"; 
	}


	public String getLoginName() {
		return loginName;
	}


	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getLoginPwd() {
		return loginPwd;
	}


	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}
    
	
	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}
	

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public static void main(String[] args) {
		System.out.println(DigestUtils.md5Hex("111111"));
	}
	
	
}
